package com.show.dao.role;


import com.show.dto.sys_role.SysRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * 角色DAO
 *
 * @Auther : yxd
 * @Param :
 * @date : 2018-02-01
 */
public interface RoleRepository extends JpaRepository<SysRole, String>, JpaSpecificationExecutor<SysRole> {

    @Query(value = "SELECT\n" +
            "	GROUP_CONCAT(role_name)\n" +
            "FROM\n" +
            "	sys_role r\n" +
            "LEFT JOIN sys_emp_role rr ON r.role_id = rr.role_id\n" +
            "LEFT JOIN sys_emp e on e.emp_id = rr.emp_id \n" +
            "WHERE e.emp_id = ? GROUP BY emp_name", nativeQuery = true)
    public String getRoleName(Integer id);

    @Query(value = "SELECT\n" +
            "	GROUP_CONCAT(r.role_id)\n" +
            "FROM\n" +
            "	sys_role r\n" +
            "LEFT JOIN sys_emp_role rr ON r.role_id = rr.role_id\n" +
            "LEFT JOIN sys_emp e on e.emp_id = rr.emp_id \n" +
            "WHERE e.emp_id = ? GROUP BY emp_name", nativeQuery = true)
    public String getRoleId(Integer id);

    @Query(value = "select count(1) from sys_role where role_code=? and not EXISTS(select 'X' from sys_role t where t.role_id=?)",nativeQuery = true)
    String checkRoleCode(String roleCode, int roleId);

    @Query(value = "select count(1) from sys_role where role_name=? and not EXISTS(select 'X' from sys_role t where t.role_id=?)",nativeQuery = true)
    String checkRoleName(String roleName, int roleId);

    @Query(value = "update sys_role set sts='0' where role_id=?",nativeQuery = true)
    @Modifying
    int  deleteRole(int roleId);

    @Query(value = "select * from  sys_role where role_id=?",nativeQuery = true)
    SysRole  findOne(int roleId);

}
